package controller

import (
	"blog/dao"
	"github.com/julienschmidt/httprouter"
	"html/template"
	"net/http"
)

//显示添加页面
func AddLinkHandle(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	dao.CommonAction(r, w)
	t, err := template.New("addLink.html").Funcs(dao.FuncMap).ParseFiles("templates/addLink.html", "templates/pub_inc.html")
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	type Data struct {
		PageName string
	}
	var D Data
	D.PageName = "友链申请"

	t.Execute(w, D)
}

//执行添加
func ToAddLinkHandle(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	dao.CommonAction(r, w)

	var lk dao.Link
	//表单检查
	r.ParseForm()
	err := lk.CheckForm(r.Form)
	if err != nil {
		dao.RedirectPage(w, "Error", err.Error(), "/addLink", 502, 3)
		return
	}
	//前台添加的不直接显示
	lk.IsShow = 0

	//执行添加
	err = lk.Add()
	if err != nil {
		dao.RedirectPage(w, "Error", "添加失败:"+err.Error(), "/addLink", 502, 3)
		return
	}

	//网页跳转
	dao.RedirectPage(w, "Success", "您的友情链接申请已经提交，请等待管理员审核", "/", 200, 3)
}
